﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Sql;
using System.Data.SqlClient;


namespace KaoWuYou.Models
{
    public class SubjectModel
    {
        public List<SubclassModel> Subclasses
        { set; get; }
        [Required]
        public int SubjectID
        { set; get; }
        [Required]
        public string SubjectName
        { set; get; }

        public void GetOwnedSubclasses()
        {
            this.Subclasses = new List<SubclassModel>();

            SqlConnection conn = new SqlConnection(Systems.ConnString);
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "SELECT * FROM SubClass WHERE SubjectID = @SubjectID";
            cmd.Parameters.AddRange(new SqlParameter[] 
            { 
                new SqlParameter("@SubjectID", SubjectID) 
            });
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.NextResult())
            {
                SubclassModel tmp = new SubclassModel()
                {
                    SubclassID = (int)sdr["SubClassID"],
                    SubclassName = sdr["SubClassName"] as string
                    
                };
                this.Subclasses.Add(tmp);
            }

            sdr.Close();
            conn.Close();
        }
    }
}